<script setup>
import { ref, onMounted } from 'vue';
import { ElRow, ElCol, ElCard, ElEmpty, ElMessage, ElSkeleton } from 'element-plus';
import axios from 'axios';

const listData = ref([]);
const loading = ref(false);
const fetchListData = async () => {
  try {
    const response = await axios.get('http://localhost:8080/carouse')
    if (response.data && response.data.code === 200) {
      listData.value = response.data.data || []
      console.log('获取到的图片数据:', listData.value);
      if (listData.value.length === 0) {
        ElMessage.info('暂无数据~');
      }
    } else {
      ElMessage.warning('数据获取失败，请重试');
    }
  } catch (error) {
    ElMessage.error('网络错误，数据加载失败');
    console.error('列表数据请求错误:', error);
  } finally {
    loading.value = false;
  }
};
onMounted(() => {
  fetchListData();
});
</script>
<template>
  <div class="list-container">
    <el-skeleton 
      :loading="loading" 
      row="4" 
      :columns="3" 
      animated 
      v-if="loading" 
    />
    <el-row :gutter="20" v-else>
      <el-col :span="8" v-for="item in listData" :key="item.id">
        <el-card class="list-card">
          <img 
            :src="item.imgUrl"  
            :alt="item.title" 
            class="list-img" 
            @click="handkeImgClick"
          />
          <div class="card-title">{{ item.title }}</div>
        </el-card>
      </el-col>
      <el-col :span="24" v-if="listData.length === 0">
        <el-empty description="暂无数据"></el-empty>
      </el-col>
    </el-row>
  </div>
</template>
<style scoped>
.list-container {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
}

.list-card {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.list-img {
  width: 100%;
  height: 200px;
  object-fit: cover;
  border-radius: 4px 4px 0 0;
}

.card-title {
  padding: 10px;
  text-align: center;
  font-size: 16px;
  font-weight: 500;
}
.list-img {
  width: 100%;
  height: 300px;
  object-fit: cover;
  transition: all 0.3s ease;
}

.list-img:hover {
  transform: scale(1.05);
  filter: grayscale(0.7);
  cursor: pointer; 
}
</style>